import request, { headers } from '@/utils/request'
import qs from 'qs'
import {downloadHandle} from '@/utils'

export default {
  version() { return request.get('/version') },
  versionDetail() { return request.get('/version/detail') },

  // 连接管理
  modules() { return request.get('/connect/modules') },
  moduleConnects(module) { return request.get(`/connect/${module}/names`) },
  connects() { return request.get('/connect/all') },
  content(module, connName) { return request.get(`/connect/${module}/${connName}`) },
  createConnect(module, params) { return request.post(`/connect/create/${module}`, params, headers.json) },
  createKafkaConnect(yamlConfig) { return request.post('/kafka/connect/create', yamlConfig, headers.plain) },
  createModule(module) { return request.post('/connect/createModule', qs.stringify({ name: module })) },
  deleteModule(module) { return request.post('/connect/deleteModule', qs.stringify({ name: module })) },
  dropConnect(module, connect) { return request.post(`/connect/dropConnect/${module}/${connect}`) },
  example(module, format) { return request.get(`/connect/${module}/${format}/example`) },

  // 安全连接管理
  security: {
    modules() { return request.get('/security/connect/modules') },
    connects() { return request.get('/security/connect/connects') },
    moduleConnects(module) { return request.get(`/security/connect/${module}/connects`, { params: { module }}) },
    moduleConnectNames(module) { return request.get(`/security/connect/${module}/connectNames`, { params: { module }}) },
    loadContent(module, baseName) { return request.get('/security/connect/loadContent', { params: { module, baseName }}) },
    writeConfig(connectInput) { return request.post('/security/connect/writeConfig', connectInput, headers.json) },
    delConnect(module, baseName) { return request.post('/security/connect/delConnect', qs.stringify({ module, baseName })) },
    connectTemplate(module) { return request.get(`/security/connect/${module}/template`) }
  },

  // 类加载器管理
  uploadSingle(classloaderName, formData) { return request.post(`/classloader/${classloaderName}/upload/single`, formData) },
  uploadMulti(formData,settings) { return request.post(`/classloader/upload/multi?settings=${settings}`, formData) },
  uploadContent(classloaderName, content,settings) { return request.post(`/classloader/${classloaderName}/upload/content?settings=${settings}`, content, headers.plain) },
  classloaders() { return request.get('/classloader/classloaders') },
  listLoadedClasses(classloaderName) { return request.get('/classloader/listLoadedClasses', { params: { classloaderName }}) },
  methodNames(classloaderName, className) { return request.get(`/classloader/${classloaderName}/${className}/methodNames`) },
  methodInfo(classloaderName, className, methodName) { return request.get(`/classloader/${classloaderName}/${className}/${methodName}/methodInfo`) },
  deCompileClass(classloaderName, className) { return request.get('/classloader/deCompileClass', { params: { classloaderName, className }}) },
  buildMethodParams(classloaderName, className, methodName) { return request.get(`/classloader/${classloaderName}/${className}/${methodName}/buildParams`) },
  loaderTreeFiles(classloaderName){return request.get('/classloader/loaderFilesTree',{params: {classloaderName}})},
  loadClass(classloaderName, className){return request.get('/classloader/loadClass',{params: {classloaderName,className}})},

  // 插件管理
  plugins() { return request.get('/plugin/list') },
  pluginDetail(pluginId) { return request.get('/plugin/detail', { params: { pluginId }}) },
  visited(pluginId) { return request.get('/plugin/visited', { params: { pluginId }}) },
  serializer() { return request.get('/plugin/serializer') },
  hasModule(moduleId){return request.get('/hasModule',{params:{ moduleId }})},

  // 序列化工具
  serializers() { return request.get('/serializer/names') },

  // 随机数据
  randomData(className, classloaderName) { return request.get('/data/random', { params: { className, classloaderName }}) },
  randomListData(className, classloaderName, length) { return request.get('/data/random/list', { params: { className, classloaderName, length }}) },
  randomRegexData(regexBase64, length) { return request.post('/data/random/regex', qs.stringify({ regexBase64, length }), headers.urlencoded) },
  spiderData(spiderDataParam) { return request.post('/data/spider', spiderDataParam, headers.json) },
  spelData(spel,length){return request.get('/data/spelData',{params:{spel,length}})},

  // 文件管理
  fileDownload(baseName, filename) {
    baseName = baseName.replace(/\\/g, '/')
    this.download('/file/manager/download?baseName=' + baseName, { filename })
  },
  downloadPublicKey() {
    this.download('/system/download/publicKey')
  },
  childNames(relativePath) { return request.get('/file/manager/childNames', { params: { relativePath }}) },
  calcDirectorySize(relativePath) { return request.get('/file/manager/calcDirectorySize', { params: { relativePath }}) },
  deleteFiles(relativePath) { return request.post('/file/manager/deleteFiles', qs.stringify({ relativePath }), headers.urlencoded) },

  // 通用下载
  download(url, params) {
    request({
      method: 'get',
      url: url,
      params: params,
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(res => {
      downloadHandle(res,params)
    }).catch((error) => {
      console.log(error)
    })
  }
}
